use hypers::prelude::*;
use hypers::tracing::info;

// Websocket  ws://127.0.0.1:7878/ws  http://www.jsons.cn/websocket/
pub async fn ws(_req: Request, mut ws: WebSocket) {
    while let Some(msg) = ws.receive().await {
        match msg {
            Message::Text(text) => {
                ws.send(Message::Text(text)).await;
            }
            Message::Close(_) => break,
            _ => {}
        }
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt().init();
    let mut root = Router::default();
    root.ws("/ws", ws);
    info!("root = {:#?}", root);
    let listener = hypers::TcpListener::bind("127.0.0.1:7878").await?;
    hypers::listen(root, listener).await
}

